package server

import (
	wallet2 "gitee.com/enjoy_5/enjoy-wallet/model/wallet"
	wallet "gitee.com/enjoy_5/enjoy-wallet/proto/wallet"
)

func WalletRechargePbToModel(in *wallet.RechargeRecordInfo) *wallet2.RechargeRecord {
	return &wallet2.RechargeRecord{
		UserID:         in.GetUserid(),
		RechargeAmount: in.GetRechargeAmount(),
		BonusAmount:    in.GetBonusAmount(),
	}
}

func WalletRechargeModelToPb(in *wallet2.RechargeRecord) *wallet.RechargeRecordInfo {
	return &wallet.RechargeRecordInfo{
		Userid:         in.UserID,
		RechargeAmount: in.RechargeAmount,
		BonusAmount:    in.BonusAmount,
	}
}

func WalletUserBalancePbToModel(in *wallet.UserBalanceInfo) *wallet2.UserBalance {
	return &wallet2.UserBalance{
		UserID:  in.GetUserid(),
		Balance: in.GetBalance(),
	}
}

func WalletUserBalanceModelToPb(in *wallet2.UserBalance) *wallet.UserBalanceInfo {
	return &wallet.UserBalanceInfo{
		Userid:  in.UserID,
		Balance: in.Balance,
	}
}

func WalletPbToModel(in *wallet.UserConsumptionDetailsInfo) *wallet2.UserConsumptionDetails {
	return &wallet2.UserConsumptionDetails{
		UserID:          in.GetUserid(),
		TransactionType: in.TransactionType,
		Amount:          in.Amount,
		Balance:         in.Balance,
		Description:     in.Description,
	}
}

func WalletModelToPb(in *wallet2.UserConsumptionDetails) *wallet.UserConsumptionDetailsInfo {
	return &wallet.UserConsumptionDetailsInfo{
		Userid:          in.UserID,
		TransactionType: in.TransactionType,
		Amount:          in.Amount,
		Balance:         in.Balance,
		Description:     in.Description,
	}
}

func CreateAmount(in *wallet.RechargeRecordInfo) (*wallet.RechargeRecordDescription, error) {
	conf := in.UserConsumptionDetailsInfo
	mod := WalletRechargePbToModel(in)
	amount, err := mod.CreatAmount(conf.GetTransactionType(), conf.GetDescription(), conf.GetAmount(), conf.GetBalance())
	if err != nil {
		return nil, err
	}

	return &wallet.RechargeRecordDescription{
		Userid:          amount.UserID,
		RechargeAmount:  amount.RechargeAmount,
		BonusAmount:     amount.BonusAmount,
		TransactionType: amount.TransactionType,
		Description:     amount.Description,
	}, nil

}

func FirstUserBalance(id int64) (*wallet.UserBalanceInfo, error) {
	info := new(wallet2.UserBalance)
	res, err := info.FirstBalance(id)
	if err != nil {
		return nil, err
	}
	return WalletUserBalanceModelToPb(res), err
}

func CreateConsumption(in *wallet.UserConsumptionDetailsInfo) (*wallet.UserConsumptionDetailsInfo, error) {
	mod := WalletPbToModel(in)
	res, err := mod.CreatConsumption()
	if err != nil {
		return nil, err
	}

	return WalletModelToPb(res), err
}
